%% Compare Autocovariance Functions of Stationarised and Unit-Root Models
% by Jaromir Benes

% In this m-file, we show that the autocovariance functions implied by the
% unit-root model and its stationarised version are identical.

%% Clear workspace and load models

clear
close all;
home;

%% Load models
%
% Load all three versions of the model created in `read_model`:
%
% * `m1` is the stationarised version,
% * `m2` is the unit-root version solved around a point on the
% balanced-growth path where productivity `A = 1`,
% * `m3` is the unit-root version solved around a point on the
% balanced-growth path where productivity `A = 2`.

load read_model.mat m1 m2 m3;

%% Compuate autocovariance function
%
% Compute the autocovariance function up to first order (i.e. t-1 lag).
% Note that the variance-covariance of unit-root variables in models `m2`
% and `m3` (i.e. `Y`, `C`, `I`, `K`, `A`) is infinity.

c1 = acf(m1,'order',1);
c2 = acf(m2,'order',1);
c3 = acf(m3,'order',1);

size(c1)
size(c2)
size(c3)

% ...
%
% Display the contemporaneous covariances.

c1(:,:,1) %#ok<NOPTS>
c2(:,:,1) %#ok<NOPTS>
c3(:,:,1) %#ok<NOPTS>

%
%
% Display the first-order autocovariance matrix

c1(:,:,2) %#ok<NOPTS>
c2(:,:,2) %#ok<NOPTS>
c3(:,:,2) %#ok<NOPTS>

%% Select a subset of stationary variables
%
% Compare the autocovariance function of a subset of stationary variables:
% for instance, `y = Y/A`, `c = C/A`, and `r`. The size of the resulting
% matrices `c1`, `c2`, `c3`, and the ordering of the veriables in their
% rows and columns will be now identical.
%
% The differences come from rounding errors only.


c1 = select(c1,{'y','c','r'});
c2 = select(c2,{'y','c','r'});
c3 = select(c3,{'y','c','r'});

c1 - c2 %#ok<NOPTS,*MNEFF>
c1 - c3 %#ok<NOPTS>

maxabs(c1,c2)
maxabs(c1,c3)

%% Help on IRIS functions used in this m-file
%
% Use either `help` to display help in the command window, or `idoc`
% to display help in a HTML browser window.
%
%    help model/acf
%    help select
